<template>
  <div class="markdown">
    <div style="width:100%;height:auto; margin:0 auto;">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="margin: 0 auto;width:100%;">
        <el-row>
          <el-col :span="16">
            <el-form-item label="手册名称" prop="tableName">
              <el-input v-model="form.tableName" placeholder="手册名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排序号" prop="sortNum">
              <el-input-number v-model="form.sortNum" :step="1" style="width:100%;text-align:left;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.tableDetailExplain" type="textarea" :autosize="{ minRows: 2, maxRows: 2}" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容" prop="markdownData">
              <mavon-editor
                ref="md"
                v-model="content"
                style="height: 400px"
                @change="change"
                @imgAdd="imgAdd"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="text-align:center; padding-top:10px">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="danger" @click="closePage">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { edit, getInfo } from '@/api/dataDictoryFile'
import { upload } from '@/api/projectFile'
import { mavonEditor } from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'

export default {
  name: '',
  components: {
    mavonEditor
  },
  inject: ['getTreeList'],
  props: { // 第二种方式
    proid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {
        sortNum: 1
      },
      rules: {
        sortNum: [
          { required: true, message: '排序号不能为空', trigger: 'blur' }
        ],
        tableName: [
          { required: true, message: 'api名称不能为空', trigger: 'blur' }
        ],
        markdownData: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]
      },
      html: '',
      content: '',
      configs: {}
    }
  },
  created() {
    this.getDetailed()
  },
  methods: {
    getDetailed() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      getInfo({ code: this.proid, isNew: '1' }).then(response => {
        this.form = response.data[0]
        this.content = response.data[0].markdownData

        loading.close()
      }).catch(function() {
        loading.close()
      })
    },
    // 将图片上传到服务器，返回地址替换到md中
    imgAdd(pos, $file) {
      const formdata = new FormData()
      formdata.append('image', $file)
      // 访问后台服务器方法
      upload(formdata).then(res => {
        if (res.code === 20000) {
          this.$refs.md.$img2Url(pos, res.data.abstractPath)
        } else {
          this.$message.error(res.message)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 所有操作都会被解析重新渲染
    change(value, render) {
      this.form.htmlData = render
      this.form.markdownData = value
    },
    save() { // 编辑
      this.$refs.form.validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })

          edit(this.form).then(response => {
            Message({
              message: '编辑成功',
              type: 'success',
              duration: 5 * 1000
            })

            this.$emit('update:visible', false)
            this.getTreeList()
            loading.close()
          }).catch(response => {
            loading.close()
          })
        } else {
          return false
        }
      })
    },
    closePage() {
      this.$emit('update:visible', false)
    }
  }
}
</script>
